package de.recipeminer.environments;

import de.recipeminer.persistence.Db4oConfigurationProvider;
import de.recipeminer.persistence.Db4oContainer;
import de.recipeminer.tools.RMap;
import de.recipeminer.tools.Tools;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Map;

/**
 * @autor: Markus Ackermann
 */
public class ExecutionContext {
    private static Logger logger = Logger.getLogger(ExecutionContext.class);
    private FileFinder resourceFinder, configFinder;
    private Db4oContainer containerInstance;
    
    private ExecutionContext(Map envDescription) {
        RMap envDescRec = new RMap(envDescription);    
        String location = (String) envDescRec.rget("locations").get("configuration");
        //generify path location "/" => "\" when file is to be read e.g. in windows
        //location = Tools.generifyFilePath(location);
        String fallbackLoc = (String) envDescRec.rget("locations").get("configuration_shared");
        configFinder = new FileFinder(location, fallbackLoc);
        location = (String) envDescRec.rget("locations").get("resource_files");
        fallbackLoc = (String) envDescRec.rget("locations").get("resource_files_shared");
        resourceFinder = new FileFinder(location, fallbackLoc);
        String filename = (String) envDescRec.rget("files").get("db4o_filename");
        File dbFile = resourceFinder.getFile(filename);
        Db4oConfigurationProvider configProvider = new Db4oConfigurationProvider.Default();
        containerInstance = new Db4oContainer(configProvider , dbFile);
    }

    public static ExecutionContext fromDescription(Map envDesc) {
        return new ExecutionContext(envDesc);
    }

    public FileFinder getResourceFinder() {
        return resourceFinder;
    }

    public FileFinder getConfigFinder() {
        return configFinder;
    }

    public Db4oContainer getContainer() {
        return containerInstance;
    }

    public static class Names {
        public static final String PRODUCTION = "production";
        public static final String TEST = "test";
        public static final String TEST_CLEARDB = "test_cleardb";
    }
}
